from day20.models.models_stu import Stu
from day20.models.models_score import Score
from day20.models.models_emp import Emp
from day20.database.database import get_db
from typing import List, Optional
from fastapi import Depends, APIRouter, Query, FastAPI
from sqlalchemy.orm import Session
from day20.schema.schema_emp import EmpCreateScheme, EmpUpdateScheme, EmpResponseSchema
from day20.crud.crud_emp import create_employment_service, get_employment_service, get_employment_by_studentid_service,update_employment_service,delete_employment_service
import uvicorn
from day20.database.database import Base, engine


# Base.metadata.create_all(bind=engine)
# 创建FastAPI应用（启动后就能用Swagger）
emp_app = APIRouter()


# 创建学生就业记录
@emp_app.post("/emp/add", response_model=EmpResponseSchema, summary="添加学生就业信息")
def create_employment(
        employment: EmpCreateScheme,
        db: Session = Depends(get_db)
):
    return create_employment_service(employment=employment, db=db)


# 查询指定学生的就业信息
@emp_app.get("/emp/get_one", response_model=EmpResponseSchema, summary="查询单个学生就业信息")
def get_employment_by_student_id(
    # 路径参数：学生ID
    student_id: str,
    db: Session = Depends(get_db)
) -> EmpResponseSchema:
    return get_employment_by_studentid_service(student_id=student_id, db=db)
    # 注意这个地方 此处也可以复用get_emploument函数，此函数返回的是多个session对象，无法直接转换成pydantic，要不用列表输出，要不用[0]取某一个


# 模糊查询学生的就业信息，什么都不填查所有
@emp_app.get("/emp/get", response_model=List[EmpResponseSchema], summary="模糊查询就业信息")
def get_employment(
    # 查询参数 模糊查询
    company_name: Optional[str] = Query(None, description="按公司名称模糊筛选（如“科技”）"),
    min_salary: Optional[int] = Query(None, ge=0, description="最低薪资筛选（单位：元，如10000）"),
    max_salary: Optional[int] = Query(None, ge=0, description="最高薪资筛选（单位：元，如20000）"),
    db: Session = Depends(get_db)
) -> List[EmpResponseSchema]:
    return get_employment_service(company_name=company_name, min_salary=min_salary, max_salary=max_salary, db=db)
    # 注意这个地方 此处用get_emploument函数，此函数返回的是多个session对象，无法直接转换成pydantic，要用列表输出


# 更新学生就业信息
@emp_app.post("/emp/update", response_model=EmpResponseSchema, summary="更新学生就业信息")
def update_student(
        student_id: str,
        employment_update: EmpUpdateScheme,
        db: Session = Depends(get_db)
):
    return update_employment_service(student_id=student_id, employment_update=employment_update, db=db)


@emp_app.delete("/emp/del", summary="删除指定学生就业信息")
def delete_student(
        student_id: str,
        db: Session = Depends(get_db)
):
    return delete_employment_service(student_id=student_id, db=db)


# if __name__ == '__main__':
#     uvicorn.run("emps:emp_app", host="localhost", port=8001, reload=True)
